function img =SampleDeformedShape(source_name,n_orient,h)
% test code:
%{
path = ...
'D:/Projects/AoT/results_n_partition_3/tree_03/itrEM5/cluster_3/tan_97_1/canonical_59.shape';
img = SampleDeformedShape(path,18,7);
%}

p_file=fopen(source_name);
if p_file==-1
    fprintf('falied to load shape file %s',source_name);    
end
text = fgetl(p_file);
data= sscanf(text,'%f, ');
width = data(1); height = data(2);
line_data=[];
img = zeros(height,width,'uint8');
while(~feof(p_file))
    text = fgetl(p_file);
    data = sscanf(text,'%f, ');
    switch data(1)
        case 1
            %data(2:5)=[c0 r0 c1 r1]
			line_data = [line_data; data(2:5)'];
            %mask=lineMask([height width],[data(3) data(2)], [data(5) data(4)],1);
            %img = max(img, mask);
        otherwise
            frpintf('unkonw shape type found!\n');
    end
end
fclose(p_file);


center_r = height/2;
center_c = width/2;

% sample d_O dx, dy and rotation matrix for the whole shape
d_o=randi([-1,1],1);
dd_col = round(0.05*width);d_col = randi([-dd_col dd_col],1);
dd_row = round(0.05*height);d_row = randi([-dd_row dd_row],1);
ds =randi([8 10],1)/10;
% rotation matrix, and apply affine transformation
theta = d_o/n_orient*pi;
theta = 0;
R = [cos(theta) -sin(theta);sin(theta) cos(theta)];
R = ds.*R;


for i_line =1:size(line_data,1)
   tmp_line_data=R*(line_data(i_line,1:2)-[center_c center_r]+[d_col d_row])'+[center_c;center_r];
   line_data(i_line,1:2)=tmp_line_data';
   
   tmp_line_data=R*(line_data(i_line,3:4)-[center_c center_r]+[d_col d_row])'+[center_c;center_r];
   line_data(i_line,3:4)=tmp_line_data';
end

%% after shape rotataion, perform line deformation

for i_line =1:size(line_data,1)
    tmp_line = line_data(i_line,:);
    center = mean(reshape(tmp_line,2,2),2);

    
    d_theta_line = randi([-1 1],1)/n_orient*pi;d_theta_line = 0;
    d_line= randi([-3 3],1,2);
    d_line = [d_line d_line];
    tmp_line = tmp_line + d_line;

    dl = rotateLine(tmp_line,center(1),center(2),d_theta_line);
    line_length = (dl(1)-dl(3))^2+(dl(2)-dl(4))^2;
    line_length = sqrt(line_length);
    n_gabor = round(line_length/(2*h+1));
    if n_gabor ==0
        continue;
    else
        gabor_row = linspace(dl(2),dl(4),n_gabor+1);
        gabor_col = linspace(dl(1),dl(3),n_gabor+1);
    end
    for i_gabor = 1:length(gabor_row)-1
        d_c = randi([-1 1],1);
        d_r = randi([-1 1],1);
        d_theta_gabor = randi([-1 1],1)/n_orient*pi;
        
        c_c = mean(gabor_col(i_gabor:i_gabor+1));
        c_r = mean(gabor_row(i_gabor:i_gabor+1));
        tmp_gabor=[gabor_col(i_gabor)+d_c gabor_row(i_gabor)+d_r...
                   gabor_col(i_gabor+1)+d_c gabor_row(i_gabor+1)+d_r];
        d_gabor = rotateLine(tmp_gabor,c_c,c_r,d_theta_gabor);
        mask =lineMask([height width],[d_gabor(2) d_gabor(1)], [d_gabor(4) d_gabor(3)],2);
        img = max(img, mask);
    end
end
img = 255-img;

end



function line_data = rotateLine(line_data,c_c,c_r,theta)
R = [cos(theta) -sin(theta);sin(theta) cos(theta)];

for i_line =1:size(line_data,1)
   tmp_line_data=R*(line_data(i_line,1:2)-[c_c c_r])'+[c_c;c_r];
   line_data(i_line,1:2)=tmp_line_data';
   
   tmp_line_data=R*(line_data(i_line,3:4)-[c_c c_r])'+[c_c;c_r];
   line_data(i_line,3:4)=tmp_line_data';
end

end
